package cn.uexpo.lhz.controller.admin;


import cn.uexpo.lhz.biz.*;
import cn.uexpo.lhz.common.oldUtils.MsgBiz;
import cn.uexpo.lhz.domain.*;
import cn.uexpo.lhz.sms.Message;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * spring boot 容器加载后自动监听
 * @author ge
 * @date
 */
@Component
public class SendMessageRunner implements Runnable  {

	private EventBiz eventBiz;
	private EquipmentBiz equipmentBiz;
	private SendMessageBiz sendMessageBiz;
	private ManyBiz manyBiz;
	private  CamerastatusBiz camerastatusBiz;
	private  CameranotiyBiz cameranotiyBiz;
	private MsgBiz msgBiz;
	public static String name = null;
	public static String interval = null;

	private Date date = null;

	public static String getName() {
		return name;
	}

	public static void setName(String name) {
		SendMessageRunner.name = name;
	}

	public static String getInterval() {
		return interval;
	}

	public static void setInterval(String interval) {
		SendMessageRunner.interval = interval;
	}

	public SendMessageRunner(String name, String interval) {
		super();
		this.name = name;
		this.interval = interval;
	}

	public SendMessageRunner(EventBiz eventBiz, EquipmentBiz equipmentBiz, SendMessageBiz sendMessageBiz, ManyBiz manyBiz, CamerastatusBiz camerastatusBiz, CameranotiyBiz cameranotiyBiz, MsgBiz msgBiz) {
		this.eventBiz = eventBiz;
		this.equipmentBiz = equipmentBiz;
		this.sendMessageBiz = sendMessageBiz;
		this.manyBiz = manyBiz;
		this.camerastatusBiz = camerastatusBiz;
		this.cameranotiyBiz = cameranotiyBiz;
		this.msgBiz = msgBiz;
	}

	public SendMessageRunner() {
	}

	@Override
//	public void run(ApplicationArguments args) throws Exception {
	public void run() {
//		System.out.println("111111111111111111111name:"+name+"interval:"+interval);
		while (true) {
			System.out.println("name:"+name+"interval:"+interval);
			try {
				String message = "";
				if(null==name){
					Thread.sleep(60000);
				}else{
					CameranotiyExample cameranotiyExample2 = new CameranotiyExample();
					CameranotiyExample.Criteria cameranotiyCiteria2 = cameranotiyExample2.createCriteria();
					cameranotiyCiteria2.andEventNameLike(name);
					List<Integer> lists = cameranotiyBiz.selectMaxByAll(cameranotiyExample2);
					EventExample eventExample = new EventExample();
					EventExample.Criteria eventCiteria = eventExample.createCriteria();
					eventCiteria.andNameEqualTo(name);
					List<Event> events = eventBiz.selectByExample(eventExample);
					if(null!=events&&events.size()>0) {
						Event event = events.get(0);
						int eventId = event.getId();
						ManyExample manyExample = new ManyExample();
						ManyExample.Criteria manyCiteria = manyExample.createCriteria();
						manyCiteria.andXxidEqualTo(eventId);
						manyCiteria.andTagEqualTo("event_equipment");
						List<Many> manys = manyBiz.selectByExample(manyExample);
						if(null!=manys&&manys.size()>0) {
							for(Many many:manys) {
								JSONObject obj = new JSONObject();
								int equipmentId = many.getYyid();
								EquipmentExample equipmentExample = new EquipmentExample();
								EquipmentExample.Criteria equipmentCiteria = equipmentExample.createCriteria();
								equipmentCiteria.andIdEqualTo(equipmentId);
								List<Equipment> equipments = equipmentBiz.selectByExample(equipmentExample);
								if(null!=equipments&&equipments.size()>0) {
									obj.put("cameraname", equipments.get(0).getCameraname());
									String equipmentNumber = equipments.get(0).getExstr1();
									String state = null;
									if(null!=equipmentNumber&&!"".equals(equipmentNumber)) {
										CamerastatusExample camerastatusExample = new CamerastatusExample();
										CamerastatusExample.Criteria camerastatusCiteria = camerastatusExample.createCriteria();
										camerastatusCiteria.andExtstr2EqualTo(equipmentNumber);
										List<Camerastatus> camerastatus = camerastatusBiz.selectByExample(camerastatusExample);
										String meg = "";
										if(null!=camerastatus&&camerastatus.size()>0) {
											state = camerastatus.get(camerastatus.size()-1).getStatus();
											if("1".equals(state)){
												meg = "登出摄像头";
//								message = message+camera + state;
												message = message+ state;
											}else if("2".equals(state)){
												meg = "登录摄像头失败";
//								message = message+camera + state;
												message = message+ state;
											}else if("3".equals(state)){
												meg = "登出摄像头";
//								message = message+camera + state;
												message = message+ state;
											}
										}
									}
									obj.put("state", state);
								}
							}
						}
					}

					if(!"".equals(message)&&name!=null){
						//发短信
						SendMessageExample sendMessageExample = new SendMessageExample();
						SendMessageExample.Criteria sendMessageCiteria = sendMessageExample.createCriteria();
						sendMessageCiteria.andItemEqualTo(name);
						sendMessageCiteria.andStateEqualTo("0");
						List<SendMessage> sendMessages = sendMessageBiz.selectByExample(sendMessageExample);

						int no = 0 ;
						Date lDate = new Date();
						for(SendMessage sendMessage : sendMessages){
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//							Date date = sendMessage.getTime1();
							if(null==date){
								Message.sendMsg(sendMessage.getTel(), message);// 调用发短信方法
								sendMessage.setTime1(lDate);
								no = sendMessageBiz.updateByPrimaryKeySelective(sendMessage);
							}else{

								long sendDate = date.getTime();
								long lastDate = lDate.getTime();
								long diffDuration = lastDate-sendDate;
								int intervalTime = 30;
								if(null!=interval){
									intervalTime = Integer.parseInt(interval);
								}
								if(diffDuration>intervalTime*60000L){
									Message.sendMsg(sendMessage.getTel(),message);// 调用发短信方法
									sendMessage.setTime1(lDate);
									no = sendMessageBiz.updateByPrimaryKeySelective(sendMessage);
								}

							}
						}

						if(1==no){
							date = lDate;
						}
					}

					Thread.sleep(60000);
				}

			} catch (InterruptedException e) {

				e.printStackTrace();
			}

		}

	}

}
